package com.wondersgroup.model;

import com.fr.base.FRContext;
import com.fr.base.ParameterHelper;
import com.fr.data.AbstractParameterTableData;
import com.fr.general.data.DataModel;
import com.fr.script.Calculator;
import com.fr.stable.ParameterProvider;
import com.fr.stable.StringUtils;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;
import com.wondersgroup.service.DBHelper;

import javax.print.attribute.standard.JobHoldUntil;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

/**
 * Created by Xtra on 3/22 0022.
 */
public class ParQueryTableData extends AbstractParameterTableData {

    private static final String XML_ATTR_TAG = "QueryTableDataAttr";

    private String documentID;
    private String templateCode;
    private String dataAddress;
    private String datapoor;
    private String dataName;
    private String userName;
    private String passWord;

    public String getDocumentID() {
        return documentID;
    }

    public void setDocumentID(String documentID) {
        this.documentID = documentID;
    }

    public String getTemplateCode() {
        return templateCode;
    }

    public void setTemplateCode(String templateCode) {
        this.templateCode = templateCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getDataAddress() {
        return dataAddress;
    }

    public void setDataAddress(String dataAddress) {
        this.dataAddress = dataAddress;
    }

    public String getDatapoor() {
        return datapoor;
    }

    public void setDatapoor(String datapoor) {
        this.datapoor = datapoor;
    }

    public String getDataName() {
        return dataName;
    }

    public void setDataName(String dataName) {
        this.dataName = dataName;
    }

    @Override
    public DataModel createDataModel(Calculator calculator) {
        FRContext.getLogger().log(Level.INFO, "*****表数据查询！*****");
        DBHelper dbHelper = new DBHelper();
        String url = "jdbc:sqlserver://" + dataAddress + ":" + datapoor + ";databaseName=" + dataName;
        FRContext.getLogger().log(Level.INFO,"&&&&&&&&&&&&&-----------"+ url);
        Map<String, String> itemDataValue = new LinkedHashMap<>();
        dbHelper.getConnection(url, userName, passWord);
        String sql1 = "select b.c_displayTitle ,a.c_content from t_mb_emr_docValue a left JOIN t_mb_emr_items b on a.c_item=b.c_itemID where a.c_document='"+documentID+"'";
        String sql2 = "SELECT a.c_displayTitle ,[c_content]=('') from t_mb_emr_items  a WHERE a.c_template='" + templateCode + "'";
        if (!StringUtils.isEmpty(documentID)) {
            FRContext.getLogger().log(Level.INFO,"执行ＳＱＬ1－－"+sql1);
            itemDataValue = dbHelper.executeQuery(sql1);

        } else {
            FRContext.getLogger().log(Level.INFO,"执行ＳＱＬ２－－"+sql2);
            itemDataValue = dbHelper.executeQuery(sql2);
        }
        //itemDataValue = dbHelper.executeQuery(sql2);
        dbHelper.close();
        Set<String> set = itemDataValue.keySet();
        String[] itemDataName = new String[set.size()];
        //Set-->数组
        set.toArray(itemDataName);
        if(itemDataName.length<=0){
            FRContext.getLogger().log(Level.INFO, "给定信息异常，查询信息失败");
        }
        FRContext.getLogger().log(Level.INFO, "表信息查询完毕！正在生成表格");
        return new ParQueryTableModel(itemDataName, itemDataValue);
    }


    @Override
    public void readXML(XMLableReader reader) {
        super.readXML(reader);
        if (reader.isChildNode()) {
            String tagName = reader.getTagName();
            if (tagName.equals(XML_ATTR_TAG)) {
                documentID = reader.getAttrAsString("documentID", StringUtils.EMPTY);
                templateCode = reader.getAttrAsString("templateCode", StringUtils.EMPTY);
                dataAddress = reader.getAttrAsString("dataAddress", StringUtils.EMPTY);
                datapoor = reader.getAttrAsString("datapoor", StringUtils.EMPTY);
                dataName = reader.getAttrAsString("dataName", StringUtils.EMPTY);
                userName = reader.getAttrAsString("userName", StringUtils.EMPTY);
                passWord = reader.getAttrAsString("passWord", StringUtils.EMPTY);
            }
        }
    }

    @Override
    public void writeXML(XMLPrintWriter writer) {
        super.writeXML(writer);
        writer.startTAG(XML_ATTR_TAG);
        writer.attr("documentID", documentID);
        writer.attr("templateCode", templateCode);
        writer.attr("dataAddress", dataAddress);
        writer.attr("datapoor", datapoor);
        writer.attr("dataName", dataName);
        writer.attr("userName", userName);
        writer.attr("passWord", passWord);
        writer.end();
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        ParQueryTableData cloned = (ParQueryTableData) super.clone();
        cloned.documentID = documentID;
        cloned.templateCode = templateCode;
        cloned.dataAddress = dataAddress;
        cloned.datapoor = datapoor;
        cloned.dataName = dataName;
        cloned.userName = userName;
        cloned.passWord = passWord;
        return cloned;
    }
}
